package com.example.micromallsearch.controller;

import com.example.micromallsearch.bean.ProductES;
import com.example.micromallsearch.dto.CommonResult;
import com.example.micromallsearch.service.ElasticsearchTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/search")
public class TestController {

    @Autowired
    private ElasticsearchTests elasticsearchTests;

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public CommonResult<List<ProductES>> test() throws IOException {

        System.out.println("search");
        //elasticsearchTests.addIndexAndMapping2();  //添加索引，并添映射
        //elasticsearchTests.queryIndex();   //查询索引
        //elasticsearchTests.addIndex();   //添加索引
        //elasticsearchTests.deleteIndex();   //删除索引
        //elasticsearchTests.existIndex();   //检测索引是否存在
        //elasticsearchTests.addDoc1();
        //elasticsearchTests.UpdateDoc();
        //elasticsearchTests.getDoc();
        List<ProductES> productESList= elasticsearchTests.queryTerm();
        return CommonResult.success(productESList);

    }

    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public CommonResult<List<ProductES>> find(@RequestParam("keyword") String keyword) throws IOException {

        System.out.println("search:"+keyword);
        //elasticsearchTests.addIndexAndMapping2();  //添加索引，并添映射
        //elasticsearchTests.queryIndex();   //查询索引
        //elasticsearchTests.addIndex();   //添加索引
        //elasticsearchTests.deleteIndex();   //删除索引
        //elasticsearchTests.existIndex();   //检测索引是否存在
        //elasticsearchTests.addDoc1();
        //elasticsearchTests.UpdateDoc();
        //elasticsearchTests.getDoc();
        List<ProductES> productESList= elasticsearchTests.queryTerm3(keyword);
        return CommonResult.success(productESList);

    }

}
